clear
set more off
set matsize 5000

global lecind    "\\intra\partages\au_amic2\SRCV\TABLES_INITIALES\INPUT\INDIVIDUS"
global lecindsas "\\intra\partages\au_amic2\SRCV_AVRIL2021\TABLES_INITIALES_AVRIL2021\INDIVIDUS"
global ecrind    "\\intra\partages\au_amic2\SRCV_AVRIL2021\BASES_FEV2022_V5\INDIVIDUS\autres_var"
global ecrstat   "\\intra\partages\au_amic2\SRCV_AVRIL2021\STAT_fev2022_V5\2008_2019_new"

/* 1 - EXTRACTION DES VARIABLES SELECTIONNéES */
/* a - fichier STATA 1ère livraison 2008-2017 */
foreach an of numlist 8 {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 occupa situa actif actoccup pl080
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_2003
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 9 {
cd "$lecind"
use individus0`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 occupa situa actif actoccup pl080
gen annee_SRCV = 200`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_REV2_2P
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 10/11 {
cd "$lecind"
use individus`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 ///
arope nbmois_cho nbmois_sal occupa situa actif actoccup pl080
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_REV2_2P
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 12/13 {
cd "$lecind"
use individus`an'_diffv2.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip14 naf2 ///
arope nbmois_cho nbmois_sal occupa situa actif actoccup pl080
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_REV2_2P
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 14 {
cd "$lecind"
use individus`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip11 naf2 ///
arope nbmois_cho nbmois_sal occupa situa actif actoccup pl080
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf2 NAF2_REV2_2P
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

foreach an of numlist 15/17 {
cd "$lecind"
use individus`an'_diff.dta, clear
keep ind men age adultvrairep idmenc_* idindc_* /// 
sexe nbhpr statut cs24 nbtsal ressex_a ressex_b ressex_c ressex_d ressex_e ressex_f ressex_g ressex_h dip11 naf ///
arope nbmois_cho nbmois_sal occupa situa actif actoccup pl080
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
rename idmenc_* idmenc
rename idindc_* idindc
rename naf NAF2_REV2_1P
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}

/* b - fichier SAS 2nde livraison 2018-2019 */
foreach an of numlist 18/19  {
cd "$lecindsas"
use individus_20`an'.dta, clear
keep ind men age adultvrairep IDMENC_* IDINDC_* /// 
sexe nbhpr statut CS24 nbtsal RESSEX_A RESSEX_B RESSEX_C RESSEX_D RESSEX_E RESSEX_F RESSEX_G RESSEX_H DIP11 naf ///
arope NBMOIS_CHO NBMOIS_SAL occupa situa actif actoccup PL080
gen annee_SRCV = 20`an'
order men ind annee_SRCV adultvrairep 
rename IDMENC_* 	idmenc
rename IDINDC_* 	idindc
rename RESSEX_A 	ressex_a
rename RESSEX_B 	ressex_b
rename RESSEX_C 	ressex_c
rename RESSEX_D 	ressex_d
rename RESSEX_E 	ressex_e
rename RESSEX_F 	ressex_f
rename RESSEX_G 	ressex_g
rename RESSEX_H 	ressex_h
rename naf      	NAF2_REV2_1P
rename CS24     	cs24
rename DIP11    	dip11
rename NBMOIS_CHO 	nbmois_cho
rename NBMOIS_SAL 	nbmois_sal
rename PL080 	    pl080
cd "$ecrind"
save autres_indiv_`an'.dta, replace
display `an'
count 
}


/* 2 - EMPILEMENT DES TABLES */
clear 
use "$ecrind\autres_indiv_8.dta", clear
append using 	"$ecrind\autres_indiv_9.dta" "$ecrind\autres_indiv_10.dta" "$ecrind\autres_indiv_11.dta" ///
				"$ecrind\autres_indiv_12.dta" "$ecrind\autres_indiv_13.dta" "$ecrind\autres_indiv_14.dta" ///
				"$ecrind\autres_indiv_15.dta" "$ecrind\autres_indiv_16.dta" "$ecrind\autres_indiv_17.dta" ///
				"$ecrind\autres_indiv_18.dta" "$ecrind\autres_indiv_19.dta" 
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV idmenc 
save "$ecrind\autres_individus_2008_2019.dta", replace


/* 3 - MISE EN FORME DES VARIABLES */
* 3-1 DESTRING
clear
use "$ecrind\autres_individus_2008_2019.dta", clear
*situa
tab situa annee_SRCV, missing
tab situa , missing
destring situa, replace
tab situa , missing
*actif
tab actif annee_SRCV, missing
tab actif , missing
destring actif, replace
tab actif , missing
*actoccup
tab actoccup annee_SRCV, missing
tab actoccup , missing
destring actoccup, replace
tab actoccup , missing
*statut
tab statut annee_SRCV, missing
tab statut , missing
destring statut, replace
tab statut , missing
*nbtsal
tab nbtsal annee_SRCV, missing
tab nbtsal , missing
destring nbtsal, replace
tab nbtsal , missing
*sexe
tab sexe annee_SRCV, missing
tab sexe , missing
destring sexe, replace
tab sexe , missing
*DIP11
tab dip11 annee_SRCV, missing
tab dip11 , missing
destring dip11, replace
tab dip11 , missing
*DIP14
tab dip14 annee_SRCV, missing
tab dip14 , missing
destring dip14, replace
tab dip14 , missing
*CS24
tab cs24 annee_SRCV, missing
tab cs24 , missing
replace cs24="" if cs24 == "**"
tab cs24 , missing
destring cs24, replace
tab cs24 , missing
*NAF2_2003
tab NAF2_2003 annee_SRCV, missing
tab NAF2_2003 , missing
replace NAF2_2003="" if NAF2_2003 == "**"
tab NAF2_2003 , missing
destring NAF2_2003, replace
tab NAF2_2003 , missing
*NAF2_REV2_2P
tab NAF2_REV2_2P annee_SRCV, missing
tab NAF2_REV2_2P , missing
replace NAF2_REV2_2P="" if NAF2_REV2_2P == "**"
tab NAF2_REV2_2P , missing
destring NAF2_REV2_2P, replace
tab NAF2_REV2_2P , missing
save "$ecrind\autres_individus_2008_2019_def.dta", replace

* 3-2 CREATION DE DIP14_new (fusion de 2008 à 2010 des 60 et 70 en 67)
clear
use "$ecrind\autres_individus_2008_2019_def.dta", clear
gen dip14_new = dip14
replace dip14_new=67 if dip14 == 60 
replace dip14_new=67 if dip14 == 70 
tab dip14 annee_SRCV, missing
tab dip14_new annee_SRCV, missing
save "$ecrind\autres_individus_2008_2019_def.dta", replace

* 3-3 CREATION VARIABLE NUMERIQUE DE NAF2_REV2_1P et arope
clear
use "$ecrind\autres_individus_2008_2019_def.dta", clear
*naf
tab NAF2_REV2_1P annee_SRCV, missing
tab NAF2_REV2_1P , missing
gen NAF2_REV2_1P_NEW = .
replace NAF2_REV2_1P_NEW = 1  if NAF2_REV2_1P == "A"
replace NAF2_REV2_1P_NEW = 2  if NAF2_REV2_1P == "B"
replace NAF2_REV2_1P_NEW = 3  if NAF2_REV2_1P == "C"
replace NAF2_REV2_1P_NEW = 4  if NAF2_REV2_1P == "D"
replace NAF2_REV2_1P_NEW = 5  if NAF2_REV2_1P == "E"
replace NAF2_REV2_1P_NEW = 6  if NAF2_REV2_1P == "F"
replace NAF2_REV2_1P_NEW = 7  if NAF2_REV2_1P == "G"
replace NAF2_REV2_1P_NEW = 8  if NAF2_REV2_1P == "H"
replace NAF2_REV2_1P_NEW = 9  if NAF2_REV2_1P == "I"
replace NAF2_REV2_1P_NEW = 10 if NAF2_REV2_1P == "J"
replace NAF2_REV2_1P_NEW = 11 if NAF2_REV2_1P == "K"
replace NAF2_REV2_1P_NEW = 12 if NAF2_REV2_1P == "L"
replace NAF2_REV2_1P_NEW = 13 if NAF2_REV2_1P == "M"
replace NAF2_REV2_1P_NEW = 14 if NAF2_REV2_1P == "N"
replace NAF2_REV2_1P_NEW = 15 if NAF2_REV2_1P == "O"
replace NAF2_REV2_1P_NEW = 16 if NAF2_REV2_1P == "P"
replace NAF2_REV2_1P_NEW = 17 if NAF2_REV2_1P == "Q"
replace NAF2_REV2_1P_NEW = 18 if NAF2_REV2_1P == "R"
replace NAF2_REV2_1P_NEW = 19 if NAF2_REV2_1P == "S"
replace NAF2_REV2_1P_NEW = 20 if NAF2_REV2_1P == "U"
tab NAF2_REV2_1P_NEW, missing
label define L_NAF2_REV2_1P_NEW 1  "A" 2  "B" 3  "C" 4  "D" 5  "E" 6  "F" 7  "G" 8  "H" 9 "I" ///
								10 "J" 11 "K" 12 "L" 13 "M" 14 "N" 15 "O" 16 "P" 17 "Q" 18 "R" ///
								19 "S" 20 "U"
label values NAF2_REV2_1P_NEW L_NAF2_REV2_1P_NEW
tab NAF2_REV2_1P_NEW, missing
*arope
tab arope annee_SRCV, missing
tab arope , missing
gen arope_new = .
replace arope_new = 0    if arope == "000"
replace arope_new = 1    if arope == "001"
replace arope_new = 10   if arope == "010"
replace arope_new = 11   if arope == "011"
replace arope_new = 100  if arope == "100"
replace arope_new = 101  if arope == "101"
replace arope_new = 110  if arope == "110"
replace arope_new = 111  if arope == "111"
tab arope_new, missing
label define L_arope_new 0 "000" 1 "001" 10 "010" 11 "011" 100 "100" 101 "101" 110 "110" 111 "111"
label values arope_new L_arope_new
tab arope_new, missing
save "$ecrind\autres_individus_2008_2019_def.dta", replace

* 3-4 CREATION VARIABLE NUMERIQUE SITUA_NEW HOMOGENEISEE SUR TOUTE LA PERIODE AVEC 7 modalités 
clear
use "$ecrind\autres_individus_2008_2019_def.dta", clear
gen situa_new = situa
replace situa_new=7 if situa == 8 
tab situa annee_SRCV, missing
tab situa_new annee_SRCV, missing
save "$ecrind\autres_individus_2008_2019_def.dta", replace


/* 4 - CREATION DE YEAR */
clear
use "$ecrind\autres_individus_2008_2019_def.dta", clear
gen year = annee_SRCV - 1
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV year idmenc idindc age adultvrairep NAF2_2003 NAF2_REV2_2P NAF2_REV2_1P NAF2_REV2_1P_NEW dip14 dip14_new dip11 situa situa_new
save "$ecrind\autres_individus_2008_2019_def.dta", replace


/* 5 - CREATION DE LA VARIABLE DIPLOME */
clear
use "$ecrind\autres_individus_2008_2019_def.dta", clear
gen DIPLOME = .
replace DIPLOME = 1 if dip11 == 10 
replace DIPLOME = 1 if dip11 == 11
replace DIPLOME = 2 if dip11 == 30
replace DIPLOME = 3 if dip11 == 31 
replace DIPLOME = 4 if dip11 == 33
replace DIPLOME = 5 if dip11 == 41 
replace DIPLOME = 6 if dip11 == 42
replace DIPLOME = 7 if dip11 == 50 
replace DIPLOME = 8 if dip11 == 60 
replace DIPLOME = 8 if dip11 == 70  
replace DIPLOME = 9 if dip11 == 71  
tab dip11 DIPLOME, missing

replace DIPLOME = 1 if dip14_new == 10
replace DIPLOME = 1 if dip14_new == 12
replace DIPLOME = 1 if dip14_new == 20 
replace DIPLOME = 2 if dip14_new == 30 
replace DIPLOME = 3 if dip14_new == 31
replace DIPLOME = 4 if dip14_new == 33 
replace DIPLOME = 5 if dip14_new == 41 
replace DIPLOME = 6 if dip14_new == 42
replace DIPLOME = 6 if dip14_new == 43
replace DIPLOME = 6 if dip14_new == 44
replace DIPLOME = 7 if dip14_new == 50
replace DIPLOME = 8 if dip14_new == 67
replace DIPLOME = 9 if dip14_new == 71
tab dip14_new DIPLOME, missing  
 
sort  men ind annee_SRCV idmenc  
order men ind annee_SRCV year idmenc idindc age adultvrairep dip14 dip14_new dip11 DIPLOME NAF2_2003 NAF2_REV2_2P NAF2_REV2_1P NAF2_REV2_1P_NEW situa situa_new
save "$ecrind\autres_individus_2008_2019_def.dta", replace

